package com.demo.controller;

import com.demo.mapper.po.StudentPO;
import com.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Administrator on 2017/6/16 0016.
 */
@RestController
@RequestMapping(value = "/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public JSONResult add(@RequestBody StudentPO studentPO) {

        JSONResult result = new JSONResult(false,"","新增失败");
        try {
            studentService.insert(studentPO);
            result.setSuccess(true);
            result.setCode(String.valueOf(HttpStatus.OK.value()));
            result.setMessage("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("新增失败："+e.getMessage());
        }
        return result;
    }


    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public JSONResult delete(@RequestParam String id) {

        JSONResult result = new JSONResult(false,"","删除失败");
        try {
            studentService.deleteByPrimaryKey(id);
            result.setSuccess(true);
            result.setCode(String.valueOf(HttpStatus.OK.value()));
            result.setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("删除失败："+e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JSONResult update(@RequestBody StudentPO studentPO) {

        JSONResult result = new JSONResult(false,"","修改失败");
        try {
            studentService.update(studentPO);
            result.setSuccess(true);
            result.setCode(String.valueOf(HttpStatus.OK.value()));
            result.setMessage("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("修改失败："+e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public JSONResult get(@RequestParam String id) {

        JSONResult result = new JSONResult(false,"","查询失败");
        try {
            result.setData(studentService.selectByPrimaryKey(id));
            result.setSuccess(true);
            result.setCode(String.valueOf(HttpStatus.OK.value()));
            result.setMessage("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("查询失败："+e.getMessage());
        }
        return result;
    }



}
